package chat

import (
	"fmt"
	"hft/Database/admin"
	"hft/Http/Services/front/chat/chat_model"
	"hft/Http/Services/front/chat/chat_utils"
	"hft/global"
)

type Chat struct{}

// 获取楼盘信息
func (*Chat) GetLouPanInfo(louID int) (info *chat_model.LouModel, err error) {
	var louInfo *admin.LouPan
	// 如果louID为0则随机
	if louID == 0 {
		global.OC_DB.Preload("SellStatus").Preload("SellLouPanInfo").Preload("RoomType").Preload("Tags").Raw("select * from hft_loupans order by RAND() limit 1").Scan(&louInfo)
	} else {
		global.OC_DB.Preload("SellStatus").Preload("SellLouPanInfo").Preload("RoomType").Preload("Tags").First(&louInfo, louID)
	}
	var returnInfo = new(chat_model.LouModel)
	returnInfo.Name = louInfo.Name
	returnInfo.Status = louInfo.Status
	returnInfo.ReferencePrice = louInfo.ReferencePrice
	returnInfo.OpenTime = chat_utils.TransTime(louInfo.SellLouPanInfo.OpenTime)
	returnInfo.GetRoomTime = chat_utils.TransTime(louInfo.SellLouPanInfo.GetRoomTime)
	returnInfo.License = chat_utils.TransString(louInfo.SellLouPanInfo.License)
	returnInfo.RoomType = chat_utils.GetRoomType(global.OC_DB, int(louInfo.ID), louInfo.RoomType)
	returnInfo.ID = int(louInfo.ID)
	returnInfo.Address = chat_utils.GetBelongAddress(louInfo.BelongName, louInfo.Address)
	returnInfo.SellPhone = "暂无资料"
	returnInfo.SecondLine = fmt.Sprintf("%v", chat_utils.GetSecondLine(louInfo.BelongName, louInfo.MinArea, louInfo.MinArea))
	returnInfo.StatusDetail = chat_model.StatusDetail{
		StatusCode: louInfo.SellStatus.StatusCode,
		TextColor:  louInfo.SellStatus.TextColor,
		BackColor:  louInfo.SellStatus.BackColor,
		Name:       louInfo.SellStatus.Name,
	}
	for k, v := range louInfo.Tags {
		if k < 3 {
			returnInfo.Tag = append(returnInfo.Tag, v.Name)
		}
	}
	fmt.Println(returnInfo.Tag)
	returnInfo.Cover = global.OC_CONFIG.AliyunOSS.BucketUrl + louInfo.Cover
	return returnInfo, nil
}

var ChatService = new(Chat)
